import { MigrationInterface, QueryRunner } from 'typeorm';

export class InitializeFieldTypes1724356271000 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    // 创建默认字段类型
    const defaultFieldTypes = [
      {
        key: 'name',
        label: '姓名',
        type: 'text',
        required: true,
        enabled: true,
        sortOrder: 1
      },
      {
        key: 'phone',
        label: '电话',
        type: 'tel',
        required: true,
        enabled: true,
        sortOrder: 2
      },
      {
        key: 'company',
        label: '公司',
        type: 'text',
        required: false,
        enabled: true,
        sortOrder: 3
      },
      {
        key: 'personnel',
        label: '人员',
        type: 'text',
        required: false,
        enabled: true,
        sortOrder: 4
      },
      {
        key: 'region',
        label: '地区',
        type: 'text',
        required: false,
        enabled: true,
        sortOrder: 5
      },
      {
        key: 'industry',
        label: '行业',
        type: 'text',
        required: false,
        enabled: true,
        sortOrder: 6
      },
      {
        key: 'message',
        label: '留言',
        type: 'textarea',
        required: false,
        enabled: true,
        sortOrder: 7
      }
    ];

    for (const fieldType of defaultFieldTypes) {
      await queryRunner.query(
        `INSERT INTO field_type (\`key\`, label, type, required, enabled, sortOrder, createdAt, updatedAt) VALUES (?, ?, ?, ?, ?, ?, NOW(), NOW())`,
        [fieldType.key, fieldType.label, fieldType.type, fieldType.required, fieldType.enabled, fieldType.sortOrder]
      );
    }
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`DELETE FROM field_type WHERE \`key\` IN ('name', 'phone', 'company', 'personnel', 'region', 'industry', 'message')`);
  }
}